<?php

class context {

    private $data;

    const SUCCESS = "Success";
    const ERROR = "Error";
    const NONE = "None";
    const AJAX_SUCCESS = "AjaxSuccess";
    const AJAX_ERROR = "AjaxError";

    private $name;
    private static $instance = null;

    /**
     * On retourne l'instance du context. S'il n'est pas encore insancié, on l'instancie.
     * ==> Singleton : une seule instance du context.
     * @return context
     */
    public static function getInstance() {
        if (self::$instance == null)
            self::$instance = new context();
        return self::$instance;
    }

    private function __construct() {
        
    }

    public function init($name) {
        $this->name = $name;
    }

    public function getLayout() {
        return $this->layout;
    }

    public function setLayout($layout) {
        $this->layout = $layout;
    }

    public function redirect($url) {
        header("location:" . $url);
    }

    public function executeAction($action, $request) {
        $this->layout = "layout"; // creation du layout
        if (!method_exists('mainController', $action))
            return false;

        return mainController::$action($request, $this);
    }

    public function getSessionAttribute($attribute) {
        return $_SESSION[$attribute];
    }

    public function setSessionAttribute($attribute, $value) {
        $_SESSION[$attribute] = $value;
    }

    public function __get($prop) {
        return $this->data[$prop];
    }

    public function __set($prop, $value) {
        $this->data[$prop] = $value;
    }

    /**
     * Check if the request is an ajax or not
     * @author Christian DRAMSY <christian.dramsy@gmail.com>
     * @return type
     */
    public function isAjaxRequest() {
        return(isset($_SERVER['HTTP_X_REQUESTED_WITH']) &&
                strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest');
    }

}
